package com.hanthink.system.service.impl;

import com.hanthink.common.core.domain.entity.SysDept;
import com.hanthink.common.core.domain.entity.SysDictData;
import com.hanthink.common.core.domain.entity.SysRole;
import com.hanthink.common.core.domain.entity.SysUser;
import com.hanthink.common.utils.StringUtils;
import com.hanthink.common.utils.WeChatRobotUtils;
import com.hanthink.system.mapper.SysDeptMapper;
import com.hanthink.system.mapper.SysDictDataMapper;
import com.hanthink.system.mapper.SysRoleMapper;
import com.hanthink.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WeChatMessageServiceImpl {
    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysRoleMapper roleMapper;

    /**
     * 根据工厂ID获取机器人URL
     *
     * @param siteId
     * @return
     */
    public String getRobotUrlBySite(Long siteId) {
        try {
            SysDictData dictData = new SysDictData();
            dictData.setDictType("robotUrl");
            SysDept sysDept = deptMapper.selectDeptById(siteId);
            if (sysDept == null) {
                return null;
            }
            dictData.setDictLabel(sysDept.getDeptName());
            List<SysDictData> list = dictDataMapper.selectDictDataList(dictData);
            return list.isEmpty() ? null : list.get(0).getDictValue();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取推送code
     *
     * @param userName
     * @return
     */
    public String getWeChatByName(String userName) {
        SysUser sysUser = userMapper.selectUserByUserName(userName);
        if (sysUser != null) {
            return sysUser.getWechatCode();
        }
        return null;
    }

    /**
     * 获取推送用户信息
     *
     * @param siteId
     * @param roleNames
     * @return
     */
    public String getWeChatBySite(Long siteId, List<String> roleNames) {
        // 容器定义
        List<Long> roleIds = new ArrayList<>();
        String wechatCodeStr = null;
        // 获取角色id
        if (!roleNames.isEmpty()) {
            for (String name : roleNames) {
                SysRole sysRole = roleMapper.selectRoleByName(name);
                if (sysRole == null) {
                    log.error("角色名称不存在");
                    return wechatCodeStr;
                }
                roleIds.add(sysRole.getRoleId());
            }
        }
        // 通过工厂id、角色id 获取对应的用户信息
        List<SysUser> userList = userMapper.selectUserByRoleAndSite(siteId, roleIds);
        // 获取用户微信code
        if (!userList.isEmpty()) {
            wechatCodeStr = userList.stream()
                    .map(SysUser::getWechatCode)
                    .filter(Objects::nonNull)
                    .collect(Collectors.joining(","));
        }
        return wechatCodeStr;
    }


    /**
     * 发送微信消息
     *
     * @param siteId
     * @param message
     * @param roleNames
     */
    public void sendMessage(Long siteId, String message, List<String> roleNames, String userName) {
        if (siteId == null || siteId == 0L) {
            log.error("工厂信息不明确，发送消息失败！");
            return;
        }
        // 获取地址
        String robotUrl = getRobotUrlBySite(siteId);
        String weChatCode = "";
        // 获取推送人code
        if (userName != null && !userName.isEmpty()) {
            weChatCode = getWeChatByName(userName);
        } else {
            weChatCode = getWeChatBySite(siteId, roleNames);
        }
        if (StringUtils.isEmpty(robotUrl)) {
            log.error("获取机器人路径失败！");
            return;
        }
        if (StringUtils.isEmpty(weChatCode)) {
            log.error("获取推送人数据失败！");
            return;
        }
        // 处理推送
        WeChatRobotUtils.sendMessage(robotUrl, message, weChatCode);
    }
}
